<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="index_style.css"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>upload file</title>
    </head>
    <body>
        <?php

 // Check if a file has been uploaded
    if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', "", 'db_name');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ".mysqli_connect_error());
        }
        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);
        // Create the SQL query
        $query = "
            INSERT INTO `pdfs` (
                `file_name`, `type`, `size`, `data`, `date`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";
        // Execute the query
        $result = $dbLink->query($query);
        // Check if it was successfull
        if($result) { // if the query run successfully
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               ."<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           .'Error code: '. intval($_FILES['uploaded_file']['error']);
    }
    // Close the mysql connection
    $dbLink->close();
}
else {
    // if no file data recieved
    echo 'Error! A file was not sent!';
}
 
// link to back to the main page
echo '<p>Click <a href="pdfpage.php">here</a> to go back</p>';
?>
    </body>
</html>
